﻿using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Web;
using Framework;

namespace Enterprise.Admin.Handle.Download
{
    public partial class DownFile : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var id = Fq("id").SafeToInt();
            var downloadService = new DownloadService();
            var row = downloadService.GetById(id);
            string destFileName = row["CoFileName"].SafeToString();
            destFileName = Server.MapPath(DownloadConfig.RelativePath) + destFileName;
            destFileName = Server.UrlDecode(destFileName);
            if (File.Exists(destFileName))
            {
                var fi = new FileInfo(destFileName);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(row["CoName"].SafeToString()), System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", fi.Length.ToString(CultureInfo.InvariantCulture));
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(destFileName);
                Response.Flush();
                Response.End();
            }
            Response.Write(" ");
            Response.End();
        }
    }
}